﻿/*
____
 L|ypps Web controls for ASP.NET

Copyright (c) 2009 Alessandro Zifiglio, http://www.Typps.com
EMail: alessandro@Typps.com

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
namespace Typps
{
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.Design;
    using System.Drawing.Design;
    using System.Runtime.Serialization;
    using System.Text;
    using System.Web.UI;

    [CollectionDataContract]
    [KnownType(typeof(MediaItem))]
    public class MediaFrameCollection : StateManagedCollection
    {
        #region Fields

        private static readonly Type[] knownTypes;

        #endregion Fields

        #region Constructors

        static MediaFrameCollection()
        {
            MediaFrameCollection.knownTypes = new Type[] { typeof(MediaItem) };
        }

        public MediaFrameCollection()
        {
        }

        #endregion Constructors

        #region Indexers

        public MediaItem this[int index]
        {
            get
            {
                return (MediaItem)((IList)this)[index];
            }
            set
            {
                ((IList)this)[index] = value;
            }
        }

        #endregion Indexers

        #region Methods

        // Restricts to MediaItem types, items that can be added to the collection
        public int Add(MediaItem value)
        {
            return ((IList)this).Add(value);
        }

        public bool Contains(MediaItem value)
        {
            // If value is not of type MediaItem, this will return false.
            return ((IList)this).Contains(value);
        }

        public void CopyTo(MediaItem[] CssStyleItemArray, int index)
        {
            base.CopyTo(CssStyleItemArray, index);
        }

        public int IndexOf(MediaItem value)
        {
            return ((IList)this).IndexOf(value);
        }

        public void Insert(int index, MediaItem value)
        {
            ((IList)this).Insert(index, value);
        }

        public void Remove(MediaItem value)
        {
            ((IList)this).Remove(value);
        }

        protected override object CreateKnownType(int index)
        {
            return new MediaItem();
        }

        protected override Type[] GetKnownTypes()
        {
            return MediaFrameCollection.knownTypes;
        }

        protected override void OnClear()
        {
            base.OnClear();
        }

        protected override void OnInsert(int index, object value)
        {
            base.OnInsert(index, value);
            if (!(value is MediaItem))
            {
                throw new ArgumentException("value must be of type MediaItem.", "value");
            }
        }

        protected override void OnValidate(Object value)
        {
            if (value.GetType() != typeof(MediaItem))
                throw new ArgumentException("value must be of type MediaItem.", "value");
        }

        protected override void SetDirtyObject(object o)
        {
            if (o is MediaItem)
            {
                ((MediaItem)o).SetDirty();
            }
        }

        #endregion Methods
    }
}